export const request = (url, { method = 'GET', data, header }) => {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  return new Promise((resolve, reject) => {
    wx.request({
      url,
      method,
      data,
      header,
      success: function ({ data }) {
        wx.hideLoading()
        if (data.error === 0) {
          resolve(data.data)
        } else {
          reject(data.msg)
        }
      },
      fail: function (error) {
        // 失败
        wx.hideLoading()
        wx.showToast({
          title: '网络出错,稍后在试!',
          icon: 'none'
        })
        reject(error)
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  })
}

export const wxlogin = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success: function ({ code }) {
        resolve(code)
      },
      fail: function (error) {
        // 失败
        wx.showToast({
          title: '网络出错,稍后在试!',
          icon: 'none'
        })
        reject(error)
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  })
}

export const request2 = (url, { method = 'GET', data, header }) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url,
      method,
      data,
      header,
      success: function ({ data }) {
        if (data.error === 0) {
          resolve(data.data)
        } else {
          reject(data.msg)
        }
      },
      fail: function (error) {
        // 失败
        wx.showToast({
          title: '网络出错,稍后在试!',
          icon: 'none'
        })
        reject(error)
      },
      complete: function () {
      }
    })
  })
}
